﻿using System.Text;
using Peter.Common;
using Peter.Common.Interfaces;

namespace Peter.Sdw.Logic
{
   /// <summary>
   /// Model used for adding ranges.
   /// </summary>
   public class EditorRangeModel : ViewModelBase
   {
      private int m_RangeEnd;
      private int m_RangeStart;
      private string m_RangeAppend;
      private string m_RangePrepend;

      /// <summary>
      /// Gets or Sets the peter editor.
      /// </summary>
      public IPeterEditor Editor { get; set; }

      /// <summary>
      /// Gets or Sets the range prepend.
      /// </summary>
      public string RangePrepend
      {
         get { return this.m_RangePrepend; }
         set
         {
            this.m_RangePrepend = value;
            this.RaisePropertyChanged ("RangePrepend");
            this.CreateRange ();
         }
      }

      /// <summary>
      /// Gets or Sets the range append.
      /// </summary>
      public string RangeAppend
      {
         get { return this.m_RangeAppend; }
         set
         {
            this.m_RangeAppend = value;
            this.RaisePropertyChanged ("RangeAppend");
            this.CreateRange ();
         }
      }

      /// <summary>
      /// Gets or Sets the range start.
      /// </summary>
      public int RangeStart
      {
         get { return this.m_RangeStart; }
         set
         {
            this.m_RangeStart = value;
            this.RaisePropertyChanged ("RangeStart");
            this.CreateRange ();
         }
      }

      /// <summary>
      /// Gets or Sets the range end.
      /// </summary>
      public int RangeEnd
      {
         get { return this.m_RangeEnd; }
         set
         {
            this.m_RangeEnd = value;
            this.RaisePropertyChanged ("RangeEnd");
            this.CreateRange ();
         }
      }

      /// <summary>
      /// Execution of the create range command.
      /// </summary>
      private void CreateRange ()
      {
         var range = new StringBuilder ();
         for (int i = this.RangeStart; i <= this.RangeEnd; i++) {
            range.AppendLine (string.Format ("{0}{1}{2}", this.RangePrepend, i, this.RangeAppend));
         }
         this.Editor.SetText (range.ToString ().Trim ());
      }
   }
}
